class RestTimeController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [restTimeInstanceList: RestTime.list(params), restTimeInstanceTotal: RestTime.count()]
    }

    def create = {
        School school = School.get(session.schoolId)
        def schoolRestTimes = RestTime.findAllBySchool(school)

        return [restTimeInstanceList: schoolRestTimes]
    }

    def save = {

        School school = session.school
        Plan.findAllBySchool(school).each{
            it.delete(flush:true)
        }
        def schoolRestTimes = RestTime.findAllBySchool(school)
        schoolRestTimes.each{
            it.delete(flush:true)
        }
        def teacherList = Teacher.findAllBySchool(school)
        teacherList.each{
            TeacherCourseClasses.findAllByTeacher(it).each{aTeacherCourseClasses ->
                aTeacherCourseClasses.delete(flush:true)
            }
        }
        def restTimeList = params.restTimeList
        if(restTimeList){
            
            def aRestTime = restTimeList.tokenize('|')
            int i = 1;
            aRestTime.each{                
                def rT = it.tokenize(',')
                def restTimeInstance = new RestTime()
                restTimeInstance.startHour = rT[0].toInteger()
                restTimeInstance.startMinute = rT[1].toInteger()
                restTimeInstance.endHour = rT[2].toInteger()
                restTimeInstance.endMinute = rT[3].toInteger()
                restTimeInstance.unitTime = i;
                restTimeInstance.school = school
                i++
                restTimeInstance.save(flush: true) 
                
            }
            def schoolRestTimes1 = RestTime.findAllBySchool(school)
            flash.message = "保存成功"
            render(view:"create",model:[restTimeInstanceList:schoolRestTimes1])
        }else{
            flash.message = "请添加课时！"
            render(view: "create")
        } 
    }

    def show = {
        def restTimeInstance = RestTime.get(params.id)
        if (!restTimeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'restTime.label', default: 'RestTime'), params.id])}"
            redirect(action: "list")
        }
        else {
            [restTimeInstance: restTimeInstance]
        }
    }

    def edit = {
        def restTimeInstance = RestTime.get(params.id)
        if (!restTimeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'restTime.label', default: 'RestTime'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [restTimeInstance: restTimeInstance]
        }
    }

    def update = {
        def restTimeInstance = RestTime.get(params.id)
        if (restTimeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (restTimeInstance.version > version) {
                    
                    restTimeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'restTime.label', default: 'RestTime')] as Object[], "Another user has updated this RestTime while you were editing")
                    render(view: "edit", model: [restTimeInstance: restTimeInstance])
                    return
                }
            }
            restTimeInstance.properties = params
            if (!restTimeInstance.hasErrors() && restTimeInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'restTime.label', default: 'RestTime'), restTimeInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [restTimeInstance: restTimeInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'restTime.label', default: 'RestTime'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def restTimeInstance = RestTime.get(params.id)
        if (restTimeInstance) {
            try {
                restTimeInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'restTime.label', default: 'RestTime'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'restTime.label', default: 'RestTime'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'restTime.label', default: 'RestTime'), params.id])}"
            redirect(action: "list")
        }
    }
}
